package com.zxy.fmmall.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zxy.fmmall.dao.ShoppingCartMapper;
import com.zxy.fmmall.entity.ShoppingCart;
import com.zxy.fmmall.entity.ShoppingCartVo;
import com.zxy.fmmall.service.ShoppingCartService;
import com.zxy.fmmall.vo.ResultVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    @Override
    public ResultVo addShopingCart(ShoppingCart cart) {
        cart.setCartTime(sdf.format(new Date()));
        int insert = shoppingCartMapper.insert(cart);

        if (insert > 0) {
            return ResultVo.ok();
        } else return ResultVo.error();

    }

    @Override
    public ResultVo listShoppingCart(String userId) {
        List<ShoppingCartVo> shoppingCartVos = shoppingCartMapper.listShoppingCart(userId);
        return ResultVo.ok().data("shoppingCart",shoppingCartVos);
    }

    @Override
    public ResultVo listShoppingCartByCartId(String cartId) {
        ArrayList<Integer> integers = new ArrayList<>();
        String[] split = cartId.split(",");
        for (int i = 0; i < split.length; i++) {
            integers.add(Integer.parseInt(split[i]));
        }

        List<ShoppingCartVo> shoppingCartVos = shoppingCartMapper.listShoppingCartByCartId(integers);
        return ResultVo.ok().data("listCart",shoppingCartVos);
    }

    @Override
    public ResultVo updateCartNumByCartId(String cartId, String cartNum) {
        int i = shoppingCartMapper.updateCartNumByCartId(cartId, cartNum);
        if (i > 0){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }

    }
}
